package crm

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"strings"
	"time"
	"zhuandan/data_source"
	"zhuandan/models"
)

type KehusRBody struct {
	Data       []models.Crmkehu `json:"data"`
	PageNo     int              `json:"pageNo"`
	PageSize   int              `json:"pageSize"`
	TotalCount int64            `json:"totalCount"`
	TotalPage  int64            `json:"totalPage"`
}

type KehusBody struct {
	Page          int    `json:"pageNo" from:"pageNo"`
	Pagesize      int    `json:"pageSize" from:"pageSize"`
	Status        int64  `json:"status" from:"status"`
	State         int64  `json:"state" from:"state"`
	CreateTimeint string `json:"create_timeint" gorm:"create_timeint"`
	CreateTimeend string `json:"create_timeend" gorm:"create_timeend"`
	BuyTimeint    string `json:"buy_timeint" gorm:"buy_timeint"`
	BuyTimeend    string `json:"buy_timeend" gorm:"buy_timeend"`
	Provinces     string `json:"provinces" gorm:"provinces"`
	Classifys     string `json:"classifys" gorm:"classifys"`
	Uid           int64  `json:"uid" gorm:"uid"`
}

func Kehus(ctx *gin.Context) {
	request := KehusBody{}
	ctx.ShouldBind(&request)

	provinces := request.Provinces
	classifys := request.Classifys
	create_timeint := request.CreateTimeint
	create_timeend := request.CreateTimeend
	buy_timeint := request.BuyTimeint
	buy_timeend := request.BuyTimeend
	state := request.State
	currentPage := request.Page
	pageSize := request.Pagesize
	offsetNum := pageSize * (currentPage - 1)
	var count int64
	//var users []models.Procure
	var users []models.Crmkehu
	res := data_source.Db
	if state > 0 {
		res = res.Where("state = ?", state)
	}
	if buy_timeint != "" {
		res = res.Where("buy_time >= ?", buy_timeint)
	}
	if buy_timeend != "" {
		res = res.Where("buy_time < ?", buy_timeend)
	}
	if create_timeint != "" {
		res = res.Where("create_time >= ?", create_timeint)
	}
	if create_timeend != "" {
		res = res.Where("create_time < ?", create_timeend)
	}
	if provinces != "" {
		provincearr := strings.Split(provinces, ",")
		var crmkehuprovinces []models.Crmkehuprovince
		data_source.Db.Where("province in (?)", provincearr).Find(&crmkehuprovinces)
		var kehuidarr []int64
		for _, v := range crmkehuprovinces {
			kehuidarr = append(kehuidarr, v.Crmkehuid)
		}
		res = res.Where("id in (?)", kehuidarr)
	}

	if classifys != "" {
		classifyarr := strings.Split(classifys, ",")
		var crmkehuclassifys []models.Crmkehuclassify
		data_source.Db.Where("classify in (?)", classifyarr).Find(&crmkehuclassifys)
		var kehuidarr []int64
		for _, v := range crmkehuclassifys {
			kehuidarr = append(kehuidarr, v.Crmkehuid)
		}
		res = res.Where("id in (?)", kehuidarr)
	}
	res.Model(&models.Crmkehu{}).Count(&count)
	res.Limit(pageSize).Offset(offsetNum).Order("id desc").Find(&users)

	var totalPage int64
	mo := count % int64(pageSize)
	if mo > 0 {
		totalPage = count/int64(pageSize) + 1
	} else {
		totalPage = count / int64(pageSize)
	}
	data := KehusRBody{
		Data:       users,
		PageNo:     currentPage,
		PageSize:   pageSize,
		TotalCount: count,
		TotalPage:  totalPage,
	}

	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    data,
		"msg":     "ok",
	})
	return

}

func Kehulist(ctx *gin.Context) {
	request := KehusBody{}
	ctx.ShouldBind(&request)
	provinces := request.Provinces
	classifys := request.Classifys
	create_timeint := request.CreateTimeint
	create_timeend := request.CreateTimeend
	buy_timeint := request.BuyTimeint
	buy_timeend := request.BuyTimeend
	state := request.State
	uid := request.Uid
	currentPage := request.Page
	pageSize := request.Pagesize
	offsetNum := pageSize * (currentPage - 1)
	var count int64
	var users []models.Crmkehu
	res := data_source.Db
	if state == 1 {
		res = res.Where("state = 1")
	} else if state == 2 {
		res = res.Where("state = 2")
	} else if state == 3 {
		res = res.Where("state = 3").Where("crmxiaoshouid = ?", uid)
	} else if state == 4 {
		res = res.Where("state = 4").Where("crmxiaoshouid = ?", uid)
	}
	if buy_timeint != "" {
		res = res.Where("buy_time >= ?", buy_timeint)
	}
	if buy_timeend != "" {
		res = res.Where("buy_time < ?", buy_timeend)
	}
	if create_timeint != "" {
		res = res.Where("create_time >= ?", create_timeint)
	}
	if create_timeend != "" {
		res = res.Where("create_time < ?", create_timeend)
	}
	if provinces != "" {
		provincearr := strings.Split(provinces, ",")
		var crmkehuprovinces []models.Crmkehuprovince
		data_source.Db.Where("province in (?)", provincearr).Find(&crmkehuprovinces)
		var kehuidarr []int64
		for _, v := range crmkehuprovinces {
			kehuidarr = append(kehuidarr, v.Crmkehuid)
		}
		res = res.Where("id in (?)", kehuidarr)
	}

	if classifys != "" {
		classifyarr := strings.Split(classifys, ",")
		var crmkehuclassifys []models.Crmkehuclassify
		data_source.Db.Where("classify in (?)", classifyarr).Find(&crmkehuclassifys)
		var kehuidarr []int64
		for _, v := range crmkehuclassifys {
			kehuidarr = append(kehuidarr, v.Crmkehuid)
		}
		res = res.Where("id in (?)", kehuidarr)
	}
	res.Model(&models.Crmkehu{}).Count(&count)
	res.Limit(pageSize).Offset(offsetNum).Order("id desc").Find(&users)

	var totalPage int64
	mo := count % int64(pageSize)
	if mo > 0 {
		totalPage = count/int64(pageSize) + 1
	} else {
		totalPage = count / int64(pageSize)
	}
	data := KehusRBody{
		Data:       users,
		PageNo:     currentPage,
		PageSize:   pageSize,
		TotalCount: count,
		TotalPage:  totalPage,
	}

	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    data,
		"msg":     "ok",
	})
	return

}

type KehuBody struct {
	ID            int64  `json:"id" gorm:"id"`
	Uid           int64  `json:"uid" gorm:"uid"`
	Name          string `json:"name" gorm:"name"`
	Phone         string `json:"phone" gorm:"phone"`
	Provinces     string `json:"provinces" gorm:"provinces"`
	Classifys     string `json:"classifys" gorm:"classifys"`
	CreateTime    string `json:"create_time" gorm:"create_time"`
	BuyTime       string `json:"buy_time" gorm:"buy_time"`
	Status        int64  `json:"status" gorm:"status"`               // 1未成交 2已成交
	Crmxiaoshouid int64  `json:"crmxiaoshouid" gorm:"crmxiaoshouid"` // 当前销售id
	State         int64  `json:"state" gorm:"state"`                 // 1公海 2跟进中
	Page          int    `json:"pageNo" from:"pageNo"`
	Pagesize      int    `json:"pageSize" from:"pageSize"`
}

func Kehuadd(ctx *gin.Context) {
	request := KehuBody{}
	ctx.ShouldBind(&request)
	name := request.Name
	phone := request.Phone
	provinces := request.Provinces
	classifys := request.Classifys
	buy_time := request.BuyTime
	create_time := request.CreateTime
	kehu := models.Crmkehu{
		Name:       name,
		Phone:      phone,
		Classifys:  classifys,
		Provinces:  provinces,
		BuyTime:    buy_time,
		CreateTime: create_time,
		Status:     1,
		State:      1,
		Bili:       0,
	}
	data_source.Db.Create(&kehu)
	provicearr := strings.Split(provinces, ",")
	var crmkehuprovinces []models.Crmkehuprovince
	for _, p := range provicearr {
		crmkehuprovince := models.Crmkehuprovince{
			Crmkehuid: kehu.ID,
			Province:  p,
		}
		crmkehuprovinces = append(crmkehuprovinces, crmkehuprovince)
	}
	data_source.Db.Create(&crmkehuprovinces)

	classifyarr := strings.Split(classifys, ",")
	var crmkehuclassifys []models.Crmkehuclassify
	for _, p := range classifyarr {
		crmkehuclassify := models.Crmkehuclassify{
			Crmkehuid: kehu.ID,
			Classify:  p,
		}
		crmkehuclassifys = append(crmkehuclassifys, crmkehuclassify)
	}
	data_source.Db.Create(&crmkehuclassifys)
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return

}

func Kehuupdate(ctx *gin.Context) {
	request := KehuBody{}
	ctx.ShouldBind(&request)
	id := request.ID
	name := request.Name
	phone := request.Phone
	provinces := request.Provinces
	classifys := request.Classifys
	buy_time := request.BuyTime
	create_time := request.CreateTime
	kehu := models.Crmkehu{
		Name:       name,
		Phone:      phone,
		Classifys:  classifys,
		Provinces:  provinces,
		BuyTime:    buy_time,
		CreateTime: create_time,
	}
	data_source.Db.Where("id=?", id).Updates(&kehu)

	data_source.Db.Where("crmkehuid = ?", id).Delete(&models.Crmkehuprovince{})
	data_source.Db.Where("crmkehuid = ?", id).Delete(&models.Crmkehuclassify{})
	provicearr := strings.Split(provinces, ",")
	var crmkehuprovinces []models.Crmkehuprovince
	for _, p := range provicearr {
		crmkehuprovince := models.Crmkehuprovince{
			Crmkehuid: id,
			Province:  p,
		}
		crmkehuprovinces = append(crmkehuprovinces, crmkehuprovince)
	}
	data_source.Db.Create(&crmkehuprovinces)

	classifyarr := strings.Split(classifys, ",")
	var crmkehuclassifys []models.Crmkehuclassify
	for _, p := range classifyarr {
		crmkehuclassify := models.Crmkehuclassify{
			Crmkehuid: id,
			Classify:  p,
		}
		crmkehuclassifys = append(crmkehuclassifys, crmkehuclassify)
	}
	data_source.Db.Create(&crmkehuclassifys)
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return

}
func Kehudel(ctx *gin.Context) {
	request := KehuBody{}
	ctx.ShouldBind(&request)
	id := request.ID

	data_source.Db.Where("id=?", id).Delete(&models.Crmkehu{})
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return

}

type KehuRBody struct {
	Kehu      models.Crmkehu     `json:"kehu"`
	Crmgenjin []models.Crmgenjin `json:"crmgenjin"`
}

func Kehuinfo(ctx *gin.Context) {
	request := KehuBody{}
	ctx.ShouldBind(&request)
	id := request.ID
	var kehu models.Crmkehu
	data_source.Db.Where("id = ?", id).First(&kehu)

	var genjin []models.Crmgenjin
	data_source.Db.Where("crmkehuid = ?", id).Find(&genjin)
	data := KehuRBody{
		Kehu:      kehu,
		Crmgenjin: genjin,
	}
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    data,
		"msg":     "ok",
	})
	return

}

func Kehuhuoke(ctx *gin.Context) {
	request := KehuBody{}
	ctx.ShouldBind(&request)
	id := request.ID
	uid := request.Uid
	var kehu models.Crmkehu
	data_source.Db.Where("id = ?", id).First(&kehu)
	if kehu.Status > 2 {
		ctx.JSON(http.StatusOK, gin.H{
			"code":    300,
			"error":   1,
			"success": 400,
			"data":    "on",
			"msg":     "该客户已经有人在服务",
		})
		return
	}
	crmgenjinuser := models.Crmgenjinuser{
		Crmxiaoshouid: uid,
		Crmkehuid:     id,
		Inttime:       time.Now().Unix(),
		State:         1,
	}
	data_source.Db.Create(&crmgenjinuser)

	kehuupdata := models.Crmkehu{
		State:         3,
		Crmxiaoshouid: uid,
		Genjintime:    time.Now().Unix(),
	}
	data_source.Db.Where("id = ?", id).Updates(&kehuupdata)
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return

}

type KehulsRBody struct {
	Data       []models.Crmgenjinusersql `json:"data"`
	PageNo     int                       `json:"pageNo"`
	PageSize   int                       `json:"pageSize"`
	TotalCount int64                     `json:"totalCount"`
	TotalPage  int64                     `json:"totalPage"`
}

func Kehuls(ctx *gin.Context) {
	request := KehuBody{}
	ctx.ShouldBind(&request)
	id := request.ID
	currentPage := request.Page
	pageSize := request.Pagesize
	offsetNum := pageSize * (currentPage - 1)
	var count int64
	//var users []models.Procure
	var users []models.Crmgenjinusersql
	res := data_source.Db.Model(&models.Crmgenjinuser{}).Select("crmgenjinuser.*, user.logo, user.nickname").Joins("left join user on user.id = crmgenjinuser.crmxiaoshouid").Where("crmgenjinuser.crmkehuid = ?", id)

	res.Model(&models.Crmgenjinuser{}).Count(&count)
	res.Limit(pageSize).Offset(offsetNum).Order("crmgenjinuser.id desc").Find(&users)

	var totalPage int64
	mo := count % int64(pageSize)
	if mo > 0 {
		totalPage = count/int64(pageSize) + 1
	} else {
		totalPage = count / int64(pageSize)
	}
	data := KehulsRBody{
		Data:       users,
		PageNo:     currentPage,
		PageSize:   pageSize,
		TotalCount: count,
		TotalPage:  totalPage,
	}
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    data,
		"msg":     "ok",
	})
	return

}

type XiaoshoulsRBody struct {
	Data       []models.Crmgenjinusersqltwo `json:"data"`
	PageNo     int                          `json:"pageNo"`
	PageSize   int                          `json:"pageSize"`
	TotalCount int64                        `json:"totalCount"`
	TotalPage  int64                        `json:"totalPage"`
}

func Xiaoshouls(ctx *gin.Context) {
	request := KehuBody{}
	ctx.ShouldBind(&request)
	uid := request.Uid
	currentPage := request.Page
	pageSize := request.Pagesize
	offsetNum := pageSize * (currentPage - 1)
	var count int64
	//var users []models.Procure
	var users []models.Crmgenjinusersqltwo
	res := data_source.Db.Model(&models.Crmgenjinuser{}).Select("crmgenjinuser.*, crmkehu.name, crmkehu.phone").Joins("left join crmkehu on crmkehu.id = crmgenjinuser.crmkehuid").Where("crmgenjinuser.crmxiaoshouid = ?", uid)

	res.Model(&models.Crmgenjinuser{}).Count(&count)
	res.Limit(pageSize).Offset(offsetNum).Order("crmgenjinuser.id desc").Find(&users)

	var totalPage int64
	mo := count % int64(pageSize)
	if mo > 0 {
		totalPage = count/int64(pageSize) + 1
	} else {
		totalPage = count / int64(pageSize)
	}
	data := XiaoshoulsRBody{
		Data:       users,
		PageNo:     currentPage,
		PageSize:   pageSize,
		TotalCount: count,
		TotalPage:  totalPage,
	}
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    data,
		"msg":     "ok",
	})
	return

}

type KehugenjinlsRBody struct {
	Data       []models.Crmgenjinsql `json:"data"`
	PageNo     int                   `json:"pageNo"`
	PageSize   int                   `json:"pageSize"`
	TotalCount int64                 `json:"totalCount"`
	TotalPage  int64                 `json:"totalPage"`
}

func Kehugenjinls(ctx *gin.Context) {
	request := KehuBody{}
	ctx.ShouldBind(&request)
	id := request.ID
	currentPage := request.Page
	pageSize := request.Pagesize
	offsetNum := pageSize * (currentPage - 1)
	var count int64
	//var users []models.Procure
	var users []models.Crmgenjinsql
	res := data_source.Db.Model(&models.Crmgenjin{}).Select("crmgenjin.*, user.logo, user.nickname").Joins("left join user on user.id = crmgenjin.crmxiaoshouid").Where("crmgenjin.crmkehuid = ?", id)

	res.Model(&models.Crmgenjin{}).Count(&count)
	res.Limit(pageSize).Offset(offsetNum).Order("crmgenjin.id desc").Find(&users)

	var totalPage int64
	mo := count % int64(pageSize)
	if mo > 0 {
		totalPage = count/int64(pageSize) + 1
	} else {
		totalPage = count / int64(pageSize)
	}
	data := KehugenjinlsRBody{
		Data:       users,
		PageNo:     currentPage,
		PageSize:   pageSize,
		TotalCount: count,
		TotalPage:  totalPage,
	}
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    data,
		"msg":     "ok",
	})
	return

}

func Kehushifang(ctx *gin.Context) {
	request := KehuBody{}
	ctx.ShouldBind(&request)
	id := request.ID
	uid := request.Uid
	var kehu models.Crmkehu
	data_source.Db.Where("id = ?", id).First(&kehu)
	if kehu.Crmxiaoshouid != uid {
		ctx.JSON(http.StatusOK, gin.H{
			"code":    300,
			"error":   1,
			"success": 400,
			"data":    "on",
			"msg":     "您不是当前跟进人员",
		})
		return
	}

	//data_source.Db.Model(&models.Crmgenjinuser{}).Where("crmxiaoshouid = ?", uid).Where("crmkehuid = ?", id).Update("endtime", time.Now().Unix())
	crmgenjinuser := models.Crmgenjinuser{
		Crmkehuid:     id,
		Crmxiaoshouid: uid,
		Inttime:       time.Now().Unix(),
		State:         2,
	}
	data_source.Db.Create(&crmgenjinuser)
	data_source.Db.Model(&models.Crmkehu{}).Where("id = ?", id).Update("crmxiaoshouid", 0)
	data_source.Db.Model(&models.Crmkehu{}).Where("id = ?", id).Update("state", 2)
	data_source.Db.Model(&models.Crmkehu{}).Where("id = ?", id).Update("genjintime", 0)
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return

}

type KehugenjinaddBody struct {
	ID     int64  `json:"id" gorm:"id"`
	Uid    int64  `json:"uid" gorm:"uid"`
	Title  string `json:"title" gorm:"title"`
	Images string `json:"images" gorm:"images"`
	Audio  string `json:"audio" gorm:"audio"`
}

func Kehugenjinadd(ctx *gin.Context) {
	request := KehugenjinaddBody{}
	ctx.ShouldBind(&request)
	id := request.ID
	uid := request.Uid
	title := request.Title
	images := request.Images
	audio := request.Audio
	crmgenjin := models.Crmgenjin{
		Crmxiaoshouid: uid,
		Crmkehuid:     id,
		Title:         title,
		Audio:         audio,
		Images:        images,
		Addtime:       time.Now().Unix(),
	}
	data_source.Db.Create(&crmgenjin)
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return

}
func Kehugenjininfo(ctx *gin.Context) {
	request := KehugenjinaddBody{}
	ctx.ShouldBind(&request)
	id := request.ID
	var genjin models.Crmgenjin
	data_source.Db.Where("id = ?", id).First(&genjin)
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    genjin,
		"msg":     "ok",
	})
	return

}

type CrmsuccessaddBody struct {
	ID        int64   `json:"id" gorm:"id"`
	Uid       int64   `json:"uid" gorm:"uid"`
	Price     float64 `json:"price" gorm:"price"`
	Provinces string  `json:"provinces" gorm:"provinces"`
	File      string  `json:"file" gorm:"file"`
	Cjdate    string  `json:"cjdate" gorm:"cjdate"`
	Fapiao    int64   `json:"fapiao" gorm:"fapiao"` // 是否开票 1是 2否
}

func Crmsuccessadd(ctx *gin.Context) {
	request := CrmsuccessaddBody{}
	ctx.ShouldBind(&request)
	id := request.ID
	uid := request.Uid
	price := request.Price
	file := request.File
	cjdate := request.Cjdate
	fapiao := request.Fapiao
	provinces := request.Provinces
	crmgenjin := models.Crmsuccess{
		Crmxiaoshouid: uid,
		Crmkehuid:     id,
		Addtime:       time.Now().Unix(),
		Provinces:     provinces,
		File:          file,
		Price:         price,
		Cjdate:        cjdate,
		Fapiao:        fapiao,
	}
	data_source.Db.Create(&crmgenjin)

	now := time.Now()

	// 格式化为 yyyy-mm-dd
	date := now.Format("2006-01-02")

	crmkehuup := models.Crmkehu{
		BuyTime: date,
		State:   4,
	}
	data_source.Db.Where("id = ?", id).Updates(&crmkehuup)
	crmgenjinuser := models.Crmgenjinuser{
		Crmxiaoshouid: uid,
		Crmkehuid:     id,
		Inttime:       time.Now().Unix(),
		State:         3,
	}
	data_source.Db.Create(&crmgenjinuser)
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return

}
func Crmsuccessinfo(ctx *gin.Context) {
	request := KehugenjinaddBody{}
	ctx.ShouldBind(&request)
	id := request.ID
	var genjin models.Crmsuccess
	data_source.Db.Where("crmkehuid = ?", id).First(&genjin)
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    genjin,
		"msg":     "ok",
	})
	return

}

type CrmsuccessinfolistRBody struct {
	Data       []models.Crmsuccesssql `json:"data"`
	PageNo     int                    `json:"pageNo"`
	PageSize   int                    `json:"pageSize"`
	TotalCount int64                  `json:"totalCount"`
	TotalPage  int64                  `json:"totalPage"`
}

func Crmsuccessinfolist(ctx *gin.Context) {
	request := KehuBody{}
	ctx.ShouldBind(&request)
	currentPage := request.Page
	pageSize := request.Pagesize
	offsetNum := pageSize * (currentPage - 1)
	var count int64
	var users []models.Crmsuccesssql
	res := data_source.Db.Model(&models.Crmsuccess{}).Select("crmsuccess.*, user.logo, user.nickname, crmkehu.name").Joins("left join user on user.id = crmsuccess.crmxiaoshouid").Joins("left join crmkehu on crmkehu.id = crmsuccess.crmkehuid")

	res.Model(&models.Crmsuccess{}).Count(&count)
	res.Limit(pageSize).Offset(offsetNum).Order("crmsuccess.id desc").Find(&users)

	var totalPage int64
	mo := count % int64(pageSize)
	if mo > 0 {
		totalPage = count/int64(pageSize) + 1
	} else {
		totalPage = count / int64(pageSize)
	}
	data := CrmsuccessinfolistRBody{
		Data:       users,
		PageNo:     currentPage,
		PageSize:   pageSize,
		TotalCount: count,
		TotalPage:  totalPage,
	}
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    data,
		"msg":     "ok",
	})
	return

}

type XiaoshouinfoRBody struct {
	Genjin  int64 `json:"genjin"`
	Yixiang int64 `json:"yixiang"`
	Hetong  int64 `json:"hetong"`
	Huikuan int64 `json:"huikuan"`
}

func Xiaoshouinfo(ctx *gin.Context) {
	request := KehuBody{}
	ctx.ShouldBind(&request)
	uid := request.Uid
	var kehus []models.Crmkehu
	data_source.Db.Where("crmxiaoshouid = ?", uid).Find(&kehus)
	var genjin int64
	var huikuan int64
	for _, v := range kehus {
		if v.State == 3 {
			genjin = genjin + 1
		}
		if v.State == 4 {
			huikuan = huikuan + 1

		}
	}
	data := XiaoshouinfoRBody{
		Genjin:  genjin,
		Yixiang: 0,
		Hetong:  0,
		Huikuan: huikuan,
	}
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    data,
		"msg":     "ok",
	})
	return

}
